﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utility;
using WebReport.Models;

namespace WebReport.Controllers
{
    public class YieldController : Controller
    {
        public class Yield
        {
            public string product_code { get; set; }
            public string process_name { get; set; }
            public string module_sn { get; set; }
            public DateTime test_time { get; set; }
            public string result { get; set; }
            public string item_code { get; set; }
            public string item_value { get; set; }
            public int failsum { get; set; }
            public int passsum { get; set; }
            //第一次fail
            public int primereject { get; set; }
            //复测fass
            public int retestpass { get; set; }
            
        }

        public class user
        {
            public string name { get; set; }
            public string result { get; set; }
            public DateTime test_time { get; set; }
            public string vaule { get; set; }
        }

        public IActionResult ProductYield()
        {
            //var response = HttpHelper.HttpPost("https://localhost:44304/Process/GetTestPrd?jobnum=11111&company=111", "POST", HttpContentType.application_json, string.Empty, 100);
            //JObject parm = JObject.Parse(response);
            //var list = JsonConvert.DeserializeObject<List<Yield>>(parm["ReturnData"].ToString());
            string json =  new UrlShortener().json();
            var listdata = JsonConvert.DeserializeObject<List<Yield>>(json);
            var hu = listdata.Where(p=>p.process_name == "PRE_BI").ToList();
            //var hulie = hu.Select(p=>p.result== "PASS").ToList();

            var listuser = JsonConvert.DeserializeObject<List<user>>("[{\"name\":\"张三\",\"result\":\"fass\",\"test_time\":\"2023-08-02 08:50:42\",\"vaule\":1},{\"name\":\"张三\",\"result\":\"fass\",\"test_time\":\"2023-08-02 08:45:42\",\"vaule\":2},{\"name\":\"张三\",\"result\":\"fail\",\"test_time\":\"2023-08-02 08:15:42\",\"vaule\":3},{\"name\":\"张三\",\"result\":\"fail\",\"test_time\":\"2023-08-02 08:25:42\",\"vaule\":4},{\"name\":\"李四\",\"result\":\"fass\",\"test_time\":\"2023-08-02 07:55:42\",\"vaule\":9},{\"name\":\"李四\",\"result\":\"fass\",\"test_time\":\"2023-08-02 07:40:42\",\"vaule\":8},{\"name\":\"李四\",\"result\":\"fail\",\"test_time\":\"2023-08-02 07:35:42\",\"vaule\":7},{\"name\":\"李四\",\"result\":\"fail\",\"test_time\":\"2023-08-02 07:25:42\",\"vaule\":6}]");
            var resultList1 = listuser;//搜索到的数据列表
            var result = resultList1
                .Where(item => item.result == "fail" || item.result == "fass")
                .GroupBy(item => item.name)
                .Select(group => new {
                    Name = group.Key,
                    FailCount = group.Count(item => item.result == "fail"),
                    FirstFailTime = group.OrderBy(p => p.test_time).FirstOrDefault(item => item.result == "fail")?.vaule,
                    FirstFassTime = group.FirstOrDefault(item => item.result == "fass")?.vaule,
                    TotalCount = group.Count()
                })
                .ToList();

            var result1 = resultList1
                        .GroupBy(d => d.name)
                        .Select(g => new {
                            Name = g.Key,
                            FailCount = g.Count(d => d.result == "fail"),
                            FirstFailValue = g.FirstOrDefault(d => d.result == "fail")?.vaule,
                            FirstFassValue = g.FirstOrDefault(d => d.result == "fass")?.vaule
                        })
                        .ToList();
            // 输出结果
            foreach (var item in result)
            {
                Console.WriteLine($"Name: {item.Name}");
                Console.WriteLine($"Total count: {item.TotalCount}");
                Console.WriteLine($"Fail count: {item.FailCount}");
                Console.WriteLine($"First fail time: {item.FirstFailTime}");
                Console.WriteLine($"First fass time: {item.FirstFassTime}");
                Console.WriteLine();
            }

            var list = listdata;
            var process = list.GroupBy(p => p.process_name).ToList();
            foreach (var item in process)
            {
                var resultList = item;
                var passGroups = resultList
                    .GroupBy(item => new { item.result, item.item_value })
                    .Where(group => group.Key.result == "PASS")
                    .Select(group => new {
                        ItemValue = group.Key.item_value,
                        Count = group.Count()
                    })
                    .ToList();

                var failGroups = resultList
                    .GroupBy(item => new { item.result, item.item_value })
                    .Where(group => group.Key.result == "FAIL")
                    .Select(group => new {
                        ItemValue = group.Key.item_value,
                        Count = group.Count()
                    })
                    .ToList();

                var failGroupst = resultList
                    .GroupBy(item => new { item.result, item.item_value, item.item_code, item.product_code })
                    .Where(group => group.Key.result == "FAIL")
                    .Select(group => new Yield
                    {
                        product_code = group.Key.product_code,
                        process_name = resultList.Key,
                        result = group.Key.result,
                        item_code = group.Key.item_code,
                        item_value = group.Key.item_value,
                        passsum = resultList.Select(p => p.result == "PASS").Count(),
                        failsum = group.Count(),
                        primereject = (resultList.Where(p => p.result == "FAIL" && p.item_value == group.Key.item_value).OrderBy(p => p.test_time).Count()),
                        module_sn = string.Join(",", resultList.Where(p => p.result == "FAIL" && p.item_value == group.Key.item_value).Select(x => x.module_sn).ToList()),
                        retestpass = (resultList.Where(p => p.result == "FAIL" && p.item_value == group.Key.item_value).Count() > 0 ?
                                      resultList.Where(p => p.result == "PASS" && 
                                      resultList.Where(p => p.result == "FAIL" && p.item_value == group.Key.item_value)
                                                .Select(x => x.module_sn).ToList().Contains(p.module_sn))
                                                .OrderByDescending(p => p.test_time).Count(): 0)
                    })
                    .ToList();

            }

            //坏品项
            var fallitem = list.GroupBy(p => p.item_value).ToList();

            //var process = list.GroupBy(p => p.process_name).ToList();

            var code = list.GroupBy(p => p.product_code).ToList();

            var lit = list.GroupBy(p => new { process_name=p.process_name, item_value = p.item_value}  ).ToList();
            foreach (var item in lit)
            {
                string aaa = item.Key.ToString();
                var vaule = item;
            }

            foreach (var item in code)
            {
                string aaa = item.Key.ToString();
            }

            return View();
        }

    }
}
